package org.steven.crm.app.api.processor.base;

import org.steven.crm.app.api.AppKeys;
import org.steven.crm.app.api.AppUtil;
import com.steven.common.utils.DateTimeUtil;
import com.steven.common.utils.FileUtil;
import com.steven.xweb.web.AbstractProcessor;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import org.steven.crm.core.auth.UserSession;
import org.steven.crm.core.exception.ServiceException;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class BaseProcessor extends AbstractProcessor {
	@Override
	public void startProcess() throws Exception {
	}
	
	@Override
	public void endProcess() throws Exception {
	}

	@Override
	public void makeView() throws Exception {
	}

	public void initPageInfo() {
		int pageNumber = 10;
		int pageIndex = 1;
		try {
			pageNumber = Integer.parseInt(getStringData("pageNumber"));
		} catch (Exception e) {
		}
		try {
			pageIndex = Integer.parseInt(getStringData("pageIndex"));
		} catch (Exception e) {
		}
		setStringData("pageIndex", pageIndex + "");
		setStringData("pageNumber", pageNumber + "");
	}

	protected boolean isImageFile(File f) {
		if (null == f) {
			return false;
		}

		String fileName = f.getName().toLowerCase();
		if (!fileName.endsWith(".jpeg") && !fileName.endsWith(".gif")
				&& !fileName.endsWith(".png") && !fileName.endsWith(".logo")
				&& !fileName.endsWith(".jpg")) {
			f.delete();
			return false;
		}
		return true;
	}

	/*public void uploadImageAction() throws Exception {
		File uploadFile = getUploadFile("uploadFile");
		if (uploadFile == null || !isImageFile(uploadFile)) {
			FileUtils.forceDelete(uploadFile);
			throw new ServiceException("图片格式不正确");
		}

		String fileName = uploadFile.getName();
		String uploadDir = getStringData("uploadDir");
		String subUploadDir = DateTimeUtil.getCurrentDate();
		String dir = AppKeys.UPLOAD_FILE_PATH + File.separator +  uploadDir
				+ File.separator + subUploadDir + File.separator;
		if (!new File(dir).exists()) {
			new File(dir).mkdirs();
		}
		FileUtil.moveFile(uploadFile, dir);

		fileName = uploadDir + "/" + subUploadDir + "/" + fileName;
		String src = AppUtil.getImageURL(uploadDir, fileName, 0);
		Map<String, String> image = new HashMap<>();
		image.put("src", src);
		image.put("fileName", fileName);
		setObjectData(image);
	}*/

	protected void appendResourceUrl(Map imageData, String imageKey, String imageUrlKey, String dirName, int imageType) {
		if (StringUtils.isEmpty(imageKey)) {
			imageKey = "image";
		}
		if (StringUtils.isEmpty(imageUrlKey)) {
			imageUrlKey = "src";
		}

		if (!imageData.containsKey(imageKey)) {
			return;
		}

		String imageSrc = AppUtil.getImageURL(dirName, imageData.get(imageKey).toString(), imageType);
		imageData.put(imageUrlKey, imageSrc);
	}

	protected void appendResourceUrl(List imageDatas, String imageKey, String imageUrlKey, String dirName, int imageType) {
		for (int i = 0; i < imageDatas.size(); ++i) {
			Map imageData = (Map) imageDatas.get(i);
			appendResourceUrl(imageData, imageKey, imageUrlKey, dirName, imageType);
		}
	}


	public String getLoginUserId() throws Exception {
		return UserSession.get().get("id");
	}

	public String getLoginUserSupplierId() throws Exception {
		return UserSession.get().get(AppKeys.SUPPLIER_ID_COLUMN);
	}


}